package com.urms.controller.role;

import com.github.pagehelper.PageInfo;
import com.urms.entity.QueryRoleCondition;
import com.urms.entity.Role;
import com.urms.service.RoleSerivce;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleSerivce roleSerivce;

    @GetMapping("/queryRole")
    public PageInfo<Role> selectRoleByCondition(QueryRoleCondition qrc){
        return roleSerivce.findRole(qrc);
    }

    @PostMapping("/insertRole")
    public Integer insertRole(Role role){
        return roleSerivce.insertRole(role);
    }

    @GetMapping("/deleteRoleById")
    public Integer deleteRoleById(Integer roleId){
        int i=roleSerivce.selectUserByRoleId(roleId);
        if (i>0){
            return -1;
        }
        return roleSerivce.deleteRoleById(roleId);
    }

    @PostMapping("/selectRoleByForm")
    public List<Role> selectRoleByForm(QueryRoleCondition qrc){
        List<Role> list = roleSerivce.selectRoleByForm(qrc);
        return list;
    }

    @GetMapping("/deleteRoleList")
    public Integer deleteRoleList(Integer[] roleIds){
        for (Integer roleId: roleIds
             ) {
            int i=roleSerivce.selectUserByRoleId(roleId);
            if (i>0){
                return -1;
            }
        }
        return roleSerivce.deleteRoleList(roleIds);
    }

    @PostMapping("/roleSelectByForm")
    public PageInfo<Role> roleSelectByForm(QueryRoleCondition qrc){
        return roleSerivce.roleSelectByForm(qrc);
    }

    @PostMapping("/roleUpdate")
    public Integer roleUpdate(Role role){
        return roleSerivce.roleUpdate(role);
    }
}
